open Verilog_parser

let os = output_string stdout 

let load_verilog name =
  let open_file name = 
    try open_in_bin name
    with _ -> failwith ("Couldnt open " ^ name) in
  let fin = open_file name in
  let lexbuf = Lexing.from_channel fin in
  let _ = Verilog_lexer.init_linenum () in
  let components = 
    try Verilog_parser.source_text Verilog_lexer.token lexbuf 
    with 
    | Failure(x) -> 
      failwith ("\n  Failed to read " ^ name ^ " at line " ^ (string_of_int !Verilog_lexer.lineNum) ^ "\n  " ^ x) 
    | _ -> failwith ("Failed to read " ^ name ^ " at line " ^ (string_of_int !Verilog_lexer.lineNum)) in
  let _ = close_in fin in
  components

let print_verilog m = 
  match m with
  | Module n -> 
    printf "module %s\n" n
  | MacroModule -> 
    printf "macromodule...skipping (???)\n"
  | Udp -> 
    printf "Udp...skipping (???)\n"

let _ = 
  let comps = load_verilog "hdfs/lib/parsers/test.v" in
  List.iter (fun i -> print_verilog i) comps


